package com.controller.sys.user.login;

import com.common.ErrorCodeEnum;
import com.common.utils.Constants;
import com.common.utils.MessageUtils;
import com.core.springSecurity.service.UnlockService;
import com.model.base.Result;
import com.common.exception.BusinessException;
import com.core.asyncManager.AsyncManager;
import com.core.asyncManager.factory.AsyncFactory;
import com.core.springSecurity.common.SecurityUtils;
import com.core.springSecurity.model.LoginUserDetails;
import com.core.springSecurity.service.SysLoginService;
import com.model.dto.sys.LoginController.LoginDTO;
import com.model.dto.sys.LoginController.SmsLoginDTO;
import com.model.vo.sys.login.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = {"公共"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class LoginController {

    /**
     * 账号登录服务
     */
    @Resource
    private SysLoginService loginService;

    @Resource
    private UnlockService unlockService;
    /**
     * @Author: zql
     * @Description: 密码登录
     * @param loginDTO 登录参数 账号包括 邮箱 手机号 登录名
     * @Data: 10:41 2021/5/12
     */
    @ApiOperation(value = "密码登录")
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody @Valid LoginDTO loginDTO) {
        try {
            if(!unlockService.verifyUnlockLogin(loginDTO.getUnlockLogin())) {
                throw new BusinessException(ErrorCodeEnum.UNLOCK_PASSED_CODE_VERIFY_FAILED);
            }
            String token = loginService.login(loginDTO.getUsername(), loginDTO.getPassword());
            LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
            //调用异步任务管理器创建登录日志记录任务
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginUserDetails.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
            return Result.suc(LoginVO.builder().token(token).loginUserDetails(loginUserDetails).build());
        } catch (Exception e) {
            String message = MessageUtils.message("user.login.failed");
            if (e instanceof BusinessException) {
                message = ((BusinessException) e).getErrorCodeEnum().getErrorMsg();
            }
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginDTO.getUsername(), Constants.LOGIN_FAIL, message));
            throw e;
        }
    }

    /**
     * @Author: zql
     * @Description: 手机验证码登录
     * @param smsLoginDTO 手机号
     * @Data: 10:41 2021/5/12
     */
    @ApiOperation(value = "手机验证码登录")
    @PostMapping("/sms/login")
    public Result<LoginVO> loginSms(@RequestBody @Valid SmsLoginDTO smsLoginDTO) {
        try {
            String token = loginService.smsLogin(smsLoginDTO.getMobile(), smsLoginDTO.getCode());
            LoginUserDetails loginUserDetails = SecurityUtils.getLoginUser();
            //调用异步任务管理器创建登录日志记录任务
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(loginUserDetails.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
            return Result.suc(LoginVO.builder().token(token).loginUserDetails(loginUserDetails).build());
        } catch (Exception e) {
            String message = MessageUtils.message("user.login.failed");
            if (e instanceof BusinessException) {
                message = ((BusinessException) e).getErrorCodeEnum().getErrorMsg();
            }
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(smsLoginDTO.getMobile(), Constants.LOGIN_FAIL, message));
            throw e;
        }
    }
}
